<?php

if (!defined('BASEPATH'))
    exit('No direct script access allowed');

require_once(APPPATH . '/controllers/super_admin' . EXT);
class Message extends Super_admin {

    function __construct() {
        parent::__construct();

        $this->load->model(array(
            'User_model',
            'Message_model'
        ));

        $this->load->helper(array(
            'security',
            'string',
        ));
    }

    function index() {
        $this->list_records();
    }

    /**
     * list_records
     *
     * Load information list
     *
     * @access public
     * @param void
     * @return void
     */
    function list_records() {
        $this->load->view('message/list');
    }

    function get_messages($mode = 'inbox') {
        
        $where = 'msg.message_id != "!@"';
        
        if ($this->input->post('mode') != ''){
            $mode = $this->input->post('mode');
        }

        
        if ($mode == 'inbox'){
            
            $where .= ' AND msg.to_id = "'. _session('user_id'). '"'; 
            $where .= ' AND msg.status != "Inactive" AND msg.status != "Draft" ';
            
        }else if ($mode == 'sent_items'){
            
            $where .= ' AND msg.from_id = "'. _session('user_id'). '"';
            $where .= ' AND msg.status != "Inactive" AND msg.status != "Draft"'; 
            
        }else if ($mode == 'drafts'){
            $where .= ' AND msg.from_id = "'. _session('user_id'). '"';
            $where .= ' AND msg.status != "Inactive" AND msg.status = "Draft"'; 
        }else if ($mode == 'trash'){
//            $where .= ' AND msg.from_id = "'. _session('user_id'). '"';
            $where .= ' AND msg.status = "Inactive" AND msg.from_id = "'. _session('user_id'). '"'; 
        }
        
        $select = "msg.message_id, msg.subject, msg.message";

        $page = $this->input->post('page');
        $order_by = $this->input->post('sidx') . ' ' . $this->input->post('sord');

        if ($this->input->post('sidx') == 'category')
            $order_by .= ', sub_category';

        $total = $this->Message_model->get_total($where);
        $num_pages = ceil($total / LIMIT);

        if ($page > $num_pages && $total <> 0) {
            $page = $num_pages;
        }

        $offset = (LIMIT * $page) - LIMIT;

        $args = array(
                    'select' => $select,
                    'where' =>  $where,
                    'join' => array(),
                    'limit' => LIMIT,
                    'offset' => $offset,
                    'order_by' => $order_by,
                    'resource_id' => TRUE,
        );

        $records = $this->Message_model->get_list($args);

        $jsn_records = '[]';
        if ($total > 0) {
            $jsn_records = $this->json_formatter->_run($records, 'message_id');
        }

        echo '{"page": "' . $page . '", "total": "' . $num_pages . '", "records": "' . $total . '", "rows": ' . $jsn_records . '}';
    }

    function add(){
        
        if (_session('user_type') == 'student'){
            $where = array('user_type' => 'instructor');
        }else{
            $where = array('user_type' => 'student');
        }

        $args = array(
                    'select' => 'u.*, CONCAT(u.fname, " ",u.lname) name',
                    'where' =>  $where,
                    'join' => array(),
        );

        $data =array(
            'subject' =>NULL,
            'message'=>NULL,
            'message_id'=> 0
        );

        $data['users'] = $this->User_model->get_list($args);

        $this->load->view('message/form', $data);

    }

    function edit($id){

        if (_session('user_type') == 'student'){
            $where = array('user_type' => 'instructor');
        }else{
            $where = array('user_type' => 'student');
        }


        $join_to = array(
            'table' => USERS . ' us',
            'condition' => 'us.user_id = msg.from_id',
            'join' => 'LEFT'
        );

        $args = array(
                'select' => 'u.*, CONCAT(u.fname, " ",u.lname) name',
                'where' =>  $where,
                'join' => array(),
        );

        $params = array(
                'select' => '*',
                'where' => array('message_id' => $id),
                'join' => array($join_to),
                'order_by' => NULL
        );

        $data = $this->Message_model->get_record($params);
        $data['users'] = $this->User_model->get_list($args);

        $this->load->view('message/form', $data);


    }

    function view($id){

        $join_user = array(
            'table' => USERS . ' u',
            'condition' => '1 = 1',
            'join' => 'LEFT'
        );
        $join_from = array(
            'table' => USERS . ' us',
            'condition' => 'us.user_id = msg.from_id',
            'join' => 'LEFT'
        );

        $params = array(
                'select' => '*,CONCAT(us.fname, " ",us.lname) name',
                'where' => array('message_id' => $id),
                'join' => array($join_user, $join_from),
                'order_by' => NULL
        );

        $data = $this->Message_model->get_record($params);
//
//        echo '<pre>';
//        print_r($data);

        $this->load->view('message/display', $data);


    }

    function submit(){

        $response = FALSE;
        
        $data = array(
            'to_id' => $this->input->post('recipient', TRUE),
            'from_id' => _session('user_id'),
            'subject' => $this->input->post('subject',TRUE),
            'message' => $this->input->post('message',TRUE),
            'status' => 'Active'
        );

        $this->Message_model->_insert($data);

        $msg = sprintf(lang('cms_custom'), "Message Sent!");
        $response = TRUE;

        echo json_encode($response);

    }
    
    function save_as_draft(){

        $response = TRUE;

        $data = array(
            'to_id' => $this->input->post('recipient', TRUE),
            'from_id' => _session('user_id'),
            'subject' => $this->input->post('subject',TRUE),
            'message' => $this->input->post('message',TRUE),
            'status' => 'Draft'
        );

        $this->Message_model->_insert($data);

        $msg = sprintf(lang('cms_custom'), "Message Saved!");
        
        echo json_encode($response);
        
    }

    function delete($message_id) {
        
        $where = array('message_id' => $message_id);
        
        $data = array(
            'status' => 'Inactive'
        );
        
        $this->Message_model->_update($where, $data);

        $msg = sprintf(lang('cms_custom'), "Message record successfully deleted");

        $response = TRUE;
        _success($msg, FALSE);
        echo json_encode($response);
        
    }
    
    
    
    function delete_perm($message_id) {
        

        $this->Message_model->_delete(array('message_id' => $message_id));

        $msg = sprintf(lang('cms_custom'), "Message record successfully deleted");

        $response = TRUE;
        _success($msg, FALSE);
        echo json_encode($response);
    }



}
/* End of file cms.php */
/* Location: ./application/controllers/cms.php */
